package com.tsingtree.modular.controller.view;

import com.tsingtree.base.auth.annotion.Permission;
import com.tsingtree.base.pojo.page.LayuiPageInfo;
import com.tsingtree.modular.entity.NanImMsgMaster;
import com.tsingtree.modular.model.params.NanImMsgMasterParam;
import com.tsingtree.modular.service.NanImMsgMasterService;
import com.tsingtree.roses.core.base.controller.BaseController;
import com.tsingtree.roses.core.reqres.response.ResponseData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.Locale;
import java.util.Objects;


/**
 * 私信控制器
 *
 * @author jagger
 */
@Controller
@RequestMapping("/nanImMsgMaster")
public class NanImMsgMasterController extends BaseController {

    private String PREFIX = "/nanImMsgMaster";

    @Autowired
    private NanImMsgMasterService nanImMsgMasterService;

    /**
     * 跳转到主页面
     *
     * @author jagger
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "/nanImMsgMaster.html";
    }

    /**
     * 新增页面
     *
     * @author jagger
     */
    @RequestMapping("/add")
    public String add() {
        return PREFIX + "/nanImMsgMaster_add.html";
    }

    /**
     * 编辑页面
     *
     * @author jagger
     */
    @RequestMapping("/edit")
    public String edit() {
        return PREFIX + "/nanImMsgMaster_edit.html";
    }

    /**
     * 新增接口
     *
     * @author jagger
     */
    @RequestMapping("/addItem")
    @Permission
    @ResponseBody
    public ResponseData addItem(NanImMsgMasterParam nanImMsgMasterParam) {
        if(!StringUtils.hasText(nanImMsgMasterParam.getSendTime())){
            nanImMsgMasterParam.setSendTime(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        }
        if(!StringUtils.hasText(nanImMsgMasterParam.getText()) && !StringUtils.hasText(nanImMsgMasterParam.getImage()) && !StringUtils.hasText(nanImMsgMasterParam.getVideo())){
            return ResponseData.error("请至少发送一个类型的消息，文字、图片或视频");
        }
        this.nanImMsgMasterService.add(nanImMsgMasterParam);
        return ResponseData.success();
    }

    /**
     * 编辑接口
     *
     * @author jagger
     */
    @RequestMapping("/editItem")
    @Permission
    @ResponseBody
    public ResponseData editItem(NanImMsgMasterParam nanImMsgMasterParam) {
        this.nanImMsgMasterService.update(nanImMsgMasterParam);
        return ResponseData.success();
    }

    /**
     * 删除接口
     *
     * @author jagger
     */
    //@RequestMapping("/delete")
    //@Permission
    //@ResponseBody
    public ResponseData delete(NanImMsgMasterParam nanImMsgMasterParam) {
        this.nanImMsgMasterService.delete(nanImMsgMasterParam);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     *
     * @author jagger
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(NanImMsgMasterParam nanImMsgMasterParam) {
        NanImMsgMaster detail = this.nanImMsgMasterService.getById(nanImMsgMasterParam.getId());
        return ResponseData.success(detail);
    }

    /**
     * 查询列表
     *
     * @author jagger
     */
    @ResponseBody
    @Permission
    @RequestMapping("/list")
    public LayuiPageInfo list(NanImMsgMasterParam nanImMsgMasterParam) {
        return this.nanImMsgMasterService.findPageBySpec(nanImMsgMasterParam);
    }

}


